package com.wave.traffic.listener;

import com.wave.traffic.task.HKCameraTask;
import com.wave.traffic.task.KNDRunnable;
import com.wave.traffic.task.ServerInfo;
import com.wave.traffic.util.SocketClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/***
 * @Description 交通灯服务启动监听器
 * @Author chenjian
 * @Date 2022/9/15 10:01
 */
@Slf4j
@Component
public class InitKND {


    public static Map<String, Socket> socketMap = new ConcurrentHashMap<>();

    @Autowired
    private ServerInfo serverInfo;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @PostConstruct
    public void init() {

        //启动雷视摄像机监控任务
        threadPoolTaskExecutor.execute(new HKCameraTask(serverInfo));

        //启动康耐德设备服务端
        threadPoolTaskExecutor.execute(()->{
            try {
                ServerSocket serverSocket = new ServerSocket(serverInfo.getServerPort1());
                while (true) {
                    log.info("服务端等待连接中......");
                    Socket socket = serverSocket.accept();
                    InetSocketAddress remoteSocketAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
                    socketMap.put(remoteSocketAddress.getHostName(), socket);
                    log.info("康耐德{}上线了", remoteSocketAddress.getHostName());
                    log.info("*****康耐德列表*****");
                    log.info("康耐德总数：{}", socketMap.size());
                    socketMap.forEach((k, v) -> log.info("康耐德ip：{}, 康耐德socket信息：{}", k, v));
                    threadPoolTaskExecutor.execute(new KNDRunnable(socket, serverInfo));
                }
            } catch (Exception e) {
                log.error("接受康耐德设备连接异常:{}", e.getMessage());
            }
        });

        threadPoolTaskExecutor.execute(()->{
            while (true){
                try {
                    log.info("HKCameraTask.blockingQueue1.size()={}", HKCameraTask.blockingQueue1.size());
                    Map<Socket, String> take = HKCameraTask.blockingQueue1.take();
                    SocketClientUtil.sendData(take.keySet().stream().findFirst().get(),
                            take.values().stream().findFirst().get());
                    Thread.sleep(100);
                } catch (Exception e) {
                    log.error("消费blockingQueue1异常:{}", e.getMessage());
                }
            }
        });
        threadPoolTaskExecutor.execute(()->{
            while (true){
                try {
                    log.info("HKCameraTask.blockingQueue2.size()={}", HKCameraTask.blockingQueue2.size());
                    Map<Socket, String> take = HKCameraTask.blockingQueue2.take();
                    SocketClientUtil.sendData(take.keySet().stream().findFirst().get(),
                            take.values().stream().findFirst().get());
                    Thread.sleep(100);
                } catch (Exception e) {
                    log.error("消费blockingQueue2异常:{}", e.getMessage());
                }
            }
        });
        threadPoolTaskExecutor.execute(()->{
            while (true){
                try {
                    log.info("HKCameraTask.blockingQueue3.size()={}", HKCameraTask.blockingQueue3.size());
                    Map<Socket, String> take = HKCameraTask.blockingQueue3.take();
                    SocketClientUtil.sendData(take.keySet().stream().findFirst().get(),
                            take.values().stream().findFirst().get());
                    Thread.sleep(100);
                } catch (Exception e) {
                    log.error("消费blockingQueue3异常:{}", e.getMessage());
                }
            }
        });
    }
}
